﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using ThreeByte.ActiveDeck.Config;
using System.Xml.Linq;
using System.Net;
using System.Xml;
using System.ComponentModel;
using ThreeByte.ActiveDeck.Data;
using ThreeByte.ActiveDeck.Service;
using System.Threading;
using ThreeByte.ActiveDeck.Content;
using Microsoft.Office.Interop.PowerPoint;
using ThreeByte.ActiveDeck.AddIn.Config;
using ThreeByte.Media;
using ThreeByte.ActiveDeck.AddIn.Controls;


namespace ThreeByte.ActiveDeck.Controls
{
    /// <summary>
    /// Interaction logic for SlideChooserControl.xaml
    /// </summary>
    public partial class SlideChooserControl : UserControl, ICloseable, INotifyPropertyChanged
    {

        public event EventHandler<DialogResultEventArgs> Close;
        public event PropertyChangedEventHandler PropertyChanged;

        public CoreConfig Config;

        public SlideChooserControl(CoreConfig config) {
            InitializeComponent();
            Config = config;

            Tag = "Choose Slide"; //Implemented to support customized title bar in Forms Host

            List<Slide> availableSlides = new List<Slide>();
            foreach(Slide s in config.Presentation.Slides) {
                availableSlides.Add(s);
            }

            SlidesListBox.ItemsSource = availableSlides;
           
            DoneButton.DataContext = this;
        }

        

        private void CancelButton_Click(object sender, RoutedEventArgs e) {
            if(Close != null) {
                Close(this, new DialogResultEventArgs(System.Windows.Forms.DialogResult.Cancel));
            }
        }

        public Slide SelectedSlide {
            get {
                return (SlidesListBox.SelectedItem) as Slide;
            }
        }

        private void DoneButton_Click(object sender, RoutedEventArgs e) {
            if(Close != null) {
                Close(this, new DialogResultEventArgs(System.Windows.Forms.DialogResult.OK));
            }
        }

        private void SlidesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs("SelectedSlide"));
            }

            if(e.AddedItems.Count == 1) {
                SlidePreview.Source = SlideUtil.GetPreview((Slide)(e.AddedItems[0]));
            }
        }

       
    }
}
